package com.monkeys.Utils.ALNS;

import com.monkeys.pojo.Points;
import com.monkeys.pojo.Vehicle;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ALNSMain {
    public static List<List<Points>> getALNSResult(List<Vehicle> vehicleList, List<Points> pointsList) {
        log.info("开始执行ALNS算法");
        long begintime = System.nanoTime();


        ReadTXT.InitNodeandRoute();                             //初始化节点、路线以及最优路线指标
        try {
            ReadTXT.importInfo(vehicleList, pointsList);
        } catch (Exception e2) {
            e2.printStackTrace();
        }

        Calculation.CalDistanceMatrix();                        //生成初始距离矩阵
        Calculation.CalRelatedMatrix();                         //生成节点相关性矩阵
        ReadTXT.ExtracRoutes();                                 //生成初始解路线

        //初始化破坏算子和修复算子的相关参数
        for (int i = 0; i < 2; i++) {
            Parameter.WRepair[i] = 1;
            Parameter.WDestory[i] = 1;
            Parameter.DestoryUseTime[i] = 0;
            Parameter.RepairUseTime[i] = 0;
        }

        //算法实现
        ALNS.ALNS();

        long endtime = System.nanoTime();
        double usedTime = (endtime - begintime) / (1e9);
        log.info("ALNS执行结束，耗时：" + usedTime + "s");

        List<List<Points>> resultList = new ArrayList<>();

        for (int i = 1; i <= Parameter.VehicleNumber; ++i) {
            resultList.add(new ArrayList<>());
            for (int j = 0; j < Parameter.Best_Route[i].N.size(); ++j) {
                Points points = new Points();
                Node node = Parameter.Best_Route[i].N.get(j);
                points.setId(node.id);
                points.setLng(node.lng);
                points.setLat(node.lat);
                points.setWeight(node.weight);
                points.setDeadTime(node.deadTime);
                points.setTargetId(node.targetId);
                points.setGoodsDemandId(node.goodsDemandId);

                resultList.get(i - 1).add(points);
            }
        }
        return resultList;
    }
}